Skip to content

gh-155905: Fix error handling in _testcapi helpers - #155906

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:gh-155905-testcapi-fopen
Aug 29, 2026
Merged

gh-155905: Fix error handling in _testcapi helpers#155906
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:gh-155905-testcapi-fopen

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 16, 2026

Copy link
Copy Markdown
Member

Py_fopen() sets an exception and returns NULL on error.

The pyobject_print*() helpers in Modules/_testcapi/object.c did not check the result and passed a NULL file pointer to PyObject_Print(), which segfaulted. The pymarshal_*() helpers in Modules/_testcapimodule.c called PyErr_SetFromErrno() on top of the exception which Py_fopen() had already set, which raised SystemError on a release build and aborted a debug build.

The pyobject_print*() helpers which take a single argument are converted to METH_O, and the unchecked result of PyUnicode_FromString() is checked.

Py_fopen() sets an exception and returns NULL on error.  The
pyobject_print*() helpers did not check the result and crashed, and the
pymarshal_*() helpers set a second exception on top of it.

The pyobject_print*() helpers which take a single argument now use
METH_O, and the result of PyUnicode_FromString() is now checked.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka added tests Tests in the Lib/test dir type-crash A hard crash of the interpreter, possibly with a core dump skip news needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 16, 2026
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) August 29, 2026 16:02
@serhiy-storchaka
serhiy-storchaka merged commit a175da7 into python:main Aug 29, 2026
63 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the gh-155905-testcapi-fopen branch August 29, 2026 16:03
@miss-islington-app

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Aug 29, 2026

Copy link
Copy Markdown

GH-156580 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 29, 2026
@bedevere-app

bedevere-app Bot commented Aug 29, 2026

Copy link
Copy Markdown

GH-156581 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 29, 2026
serhiy-storchaka added a commit that referenced this pull request Aug 29, 2026
…GH-156581)

Py_fopen() sets an exception and returns NULL on error.  The
pyobject_print*() helpers did not check the result and crashed, and the
pymarshal_*() helpers set a second exception on top of it.

The pyobject_print*() helpers which take a single argument now use
METH_O, and the result of PyUnicode_FromString() is now checked.
(cherry picked from commit a175da7)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news tests Tests in the Lib/test dir type-crash A hard crash of the interpreter, possibly with a core dump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant